using System;
using System.Collections;
using System.IO;
using PHSRAG.Insight21.CDS;
using PHSRAG.Insight21.COI;
using PHSRAG.Insight21.Common.Pages;
using PHSRAG.Insight21.Policy;
using PHSRAG.Utility;
using PHSRAG.SharePointUtility;

namespace PHSRAG.Insight21.Agreements
{
    /// <summary>
    /// Summary description for DownloadForm.
    /// </summary>
    public partial class AdvanceFundDownloadForm : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int docId = 0;
            int coiId = 0;
            byte[] pdfBlob = null;

            try
            {
                string FileUrl = Request.QueryString["FileUrl"];
                if (FileUrl != null)
                {
                    DocumentLibrary dl = new DocumentLibrary();
                    pdfBlob = dl.GetFileFromSharePoint(FileUrl);
                    string[] FileName = FileUrl.Split('/');
                    DownloadFile(FileName[FileName.Length - 1].ToString(), pdfBlob);
                }                
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        /// <summary>
        /// Writes the contents of the file to the response stream
        /// </summary>
        /// <param name="fname">Name of the file</param>
        /// <param name="forceDownload">Whether or not show the File Download dialogue</param>
        /// <param name="content">the contents of the file</param>
        private void DownloadFile(string fname, byte[] content)
        {
            string ext = Path.GetExtension(fname);
            string type;
            // set known types based on file extension  
            if (ext != null)
            {
                switch (ext.ToLower())
                {
                    case ".htm":
                    case ".html":
                        type = "text/HTML";
                        break;

                    case ".txt":
                        type = "text/plain";
                        break;

                    case ".doc":
                    case ".docx":
                    case ".docm":
                    case ".dot":
                    case ".dotx":
                    case ".dotm":
                    case ".rtf":
                        type = "Application/msword";
                        break;

                    case ".pdf":
                        type = "Application/pdf";
                        break;

                    case ".xls":
                    case ".xlsx":
                    case ".xlsm":
                    case ".xlt":
                    case ".xltx":
                    case ".xlsb":
                        type = "application/vnd.ms-excel";
                        break;

                    case ".ppt":
                    case ".pptx":
                    case ".pptm":
                    case ".pot":
                    case ".potx":
                    case ".potm":
                        type = "application/vnd.ms-powerpoint";
                        break;

                    default:
                        type = "application/octet-stream";
                        break;
                }
            }
            else
            {
                type = "Application/pdf";
            }

            Response.Clear();
            Response.ClearHeaders();

            Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + fname + "\"");
            if (type != "")
            {
                Response.ContentType = type;
            }

            Response.AddHeader("Content-length", Convert.ToString(content.Length));
            Response.BinaryWrite(content);
            Response.Flush();

            Response.End();
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
            //_pageCachePolicy = (Policy.ICachePolicy) Session[Policy.Names.PageCachePolicyKey];
            //_cachePolicy = (Policy.ICachePolicy) Session[Policy.Names.CachePolicyKey];
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
        }

        #endregion
    }
}